package org.jiamandan.com.strategy.impl.tram.listener;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jiamandan.com.constant.BizConstant;
import org.jiamandan.com.dto.ZDBasicDto;
import org.jiamandan.com.strategy.impl.tram.IZDListenerService;
import org.jiamandan.com.utils.CommonUtil;
import org.jiamandan.com.utils.ZDObjectUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @see:
 * @创建日期：2022-12-12 14:00
 * @功能说明： 嘉满电-YD结算消费信息
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2022-12-01 16:50	        CWJ       	创建
 * @end
 */
@Service(value = BizConstant.CmdClass.CMD_YD_03_SERVICE)
@AllArgsConstructor
@Slf4j
public class YDChargeRecordListenerService implements IZDListenerService {


//    public static void main(String[] args) {
//        System.out.println(BigDecimal.valueOf(23000 - 120 * 60).divide(BigDecimal.valueOf(3600),0,BigDecimal.ROUND_UP));
//        System.out.println(BigDecimal.valueOf(50).divide(BigDecimal.valueOf(30/60)));
//    }


    @Override
    public void doExecute(ZDBasicDto dto, Channel client) throws Exception {
        String deviceNo = StrUtil.toString(Integer.parseInt(CommonUtil.reverseHex(dto.getDeviceNo()).substring(2), 16));
        log.info("接收嘉满电-YD结算消费信息上传(CMD=03)【" + deviceNo + "】报文:" + JSON.toJSONString(dto));
        String data = dto.getData();
        Integer sumChargeTime = Integer.parseInt(CommonUtil.reverseHex(StringUtils.substring(data, 0, 4)), 16);
        BigDecimal maxChargePower = BigDecimal.valueOf(Integer.parseInt(CommonUtil.reverseHex(StringUtils.substring(data, 4, 8)), 16)).divide(BigDecimal.valueOf(10));
        BigDecimal totalElectric = BigDecimal.valueOf(Integer.parseInt(CommonUtil.reverseHex(StringUtils.substring(data, 8, 12)), 16)).divide(BigDecimal.valueOf(100));
        String gunNo = StringUtils.substring(data, 12, 14);
        String startChargeType = StringUtils.substring(data, 14, 16);
        String card = StringUtils.substring(data, 16, 24);
        String stopReason = StringUtils.substring(data, 24, 26);
        //00000000000000646495757880266752
        String tradeNo = CommonUtil.removeZero(StringUtils.substring(data, 26, 58), true);
        String maxChargePower1 = StringUtils.substring(data, 58, 62);
        String timestamp = StringUtils.substring(data, 62, 70);

        //表示充满后占用设备的时长，单位为分钟。
        String seizeSeatTime = StringUtils.substring(data, 70, 72);
        reply(dto, client);
    }

    public static void reply(ZDBasicDto dto, Channel client) {
        //应答=0成功
        String replyMsg = ZDObjectUtil.ydpushMessage("00", dto, BizConstant.Cmd.CMD_YD_03);
        log.info("应答嘉满电-YD结算消费信息上传(CMD=03)报文:" + replyMsg);
        ByteBuf buf = Unpooled.buffer(replyMsg.getBytes().length);
        buf.writeBytes(CommonUtil.hexToByteArray(replyMsg));
        ChannelFuture future = client.writeAndFlush(buf);

        //下发81指令  快速查询设备当前工作状态
        String replyMsg1 = ZDObjectUtil.ydpushMessage("", dto, BizConstant.Cmd.CMD_YD_81);
        log.info("查询设备联网状态(CMD=81)报文:" + replyMsg1);
        ByteBuf buf1 = Unpooled.buffer(replyMsg1.getBytes().length);
        buf1.writeBytes(CommonUtil.hexToByteArray(replyMsg1));
        client.writeAndFlush(buf1);
    }


    public static String secondConvertHourMinSecond(Integer second) {
        String str = "00:00:00";
        if (second == null || second < 0) {
            return str;
        }

        // 得到小时
        long h = second / 3600;
        System.out.println(h);
        str = h > 0 ? ((h < 10 ? ("0" + h) : h) + ":") : "00:";

        // 得到分钟
        long m = (second % 3600) / 60;
        str += (m < 10 ? ("0" + m) : m) + ":";

        //得到剩余秒
        long s = second % 60;
        str += (s < 10 ? ("0" + s) : s);
        return str;
    }

    static int getStopReason(int profile) {
        switch (profile) {
            case (1):
                return 75;
            case (2):
                return 76;
            case (3):
                return 77;
            case (4):
                return 78;
            case (5):
                return 79;
            case (6):
                return 80;
            case (7):
                return 81;
            case (8):
                return 82;
            case (9):
                return 83;
            case (10):
                return 84;
            case (11):
                return 85;
            case (12):
                return 86;
            case (13):
                return 87;
            case (14):
                return 88;
            case (15):
                return 89;
            default:
                return profile;
        }
    }

}
